/**
 * author: cwl
 * 属性：
 *  socketUrl: 当前连接的socket地址
 *  socket: 当前连接的socket对象
 * 方法：
 *  setUrl(url: String) ---> 设置socket连接地址，String类型
 *  sendStartPoint(x: Number, y: Number) ---> 发送开始点的x，y坐标
 *  sendMovePoint(x: Number, y: Number) ---> 发送移动点的x，y坐标
 *  receiveStartPoint(func: function) ---> 接收开始点的x, y坐标, 传入一个回调，回调有两个参数x, y
 *  receiveMovePoint(func: function) ---> 同上
 */

;
(function (global, factory) {
  global.IO = factory();
}(this, (function () {
  'use strict'
  var evenList = {
    canvas: {
      satrt: 'start',
      move: 'move',
      end: 'end',
      style: 'style',
      clear: 'clear'
    },
    talk: {
      say: 'say',
      listen: 'listen'
    },
    system: {
      name: 'name',
      title: 'title',
      myName: 'myname',
      sys: 'sys'
    }

  }
  var IO = function () {
    this.socketUrl = 'ws://localhost:3000'
    this.socket = connection(this.socketUrl);
  }

  function connection(url) {
    return io.connect(url);
  }
  var fn = IO.prototype;

  fn.setUrl = function (url) {
    this.socketUrl = url;
    this.socket = connection(this.socketUrl);
  }

  //画图事件
  fn.sendStartPoint = function (x, y) {
    this.socket.emit(evenList.canvas.satrt, {
      startX: x,
      startY: y
    });
  }
  fn.sendMovePoint = function (x, y) {
    this.socket.emit(evenList.canvas.move, {
      moveX: x,
      moveY: y
    });
  }
  fn.sendPenStyle = function (width, color) {
    this.socket.emit(evenList.canvas.style, {
      width: width,
      color: color
    });
  }
  fn.sendClear = function () {
    this.socket.emit(evenList.canvas.clear);
  }
  fn.receiveStartPoint = function (func) {
    this.socket.on(evenList.canvas.satrt, function (data) {
      func(data.startX, data.startY);
    });
  }
  fn.receiveMovePoint = function (func) {
    this.socket.on(evenList.canvas.move, function (data) {
      func(data.moveX, data.moveY);
    })
  }
  fn.receivePenStyle = function (func) {
    this.socket.on(evenList.canvas.style, function (data) {
      func(data.width, data.color);
    });
  }
  fn.receiveClear = function (func) {
    this.socket.on(evenList.canvas.clear, function () {
      func();
    });
  }

  //聊天事件
  fn.say = function (userName, text) {
    this.socket.emit(evenList.talk.say, {
      userName: userName,
      text: text
    });
  }
  fn.listen = function (func) {
    this.socket.on(evenList.talk.listen, function (data) {
      console.log(data);
      func(data.userName, data.text);
    });
  }

  //系统事件
  fn.receiveName = function (func) {
    this.socket.on(evenList.system.name, function (data) {
      func(data);
    });
  }
  fn.receiveTitle = function (func) {
    this.socket.on(evenList.system.title, function (data) {
      func(data);
    })
  }
  fn.receiveMyName = function (func) {
    this.socket.on(evenList.system.myName, function (data) {
      func(data);
    })
  }
  fn.receiveSys = function (func) {
    this.socket.on(evenList.system.sys, function (data) {
      func(data);
    })
  }
  return IO;
})));